/*
 * Board game - put the figures down and try to make a line.
 * Copyright (C)  2008 Georgi Staykov
 * 
 * This file is part of ClearTheLines.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.gstaykov.clearthelines;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class RecordsManager {
    private static final int RECORDS_TO_DISPLAY = 10;
    private static String recordsFile = "Records.dat";
    private RecordEntry[] records = null;
    private ClearTheLines ctl = null;

    public RecordsManager(ClearTheLines ctl) {
        this.ctl = ctl;
    }
    
    public void loadRecords() {
        try {
            ArrayList<RecordEntry> tempResults = new ArrayList<RecordEntry>();
            BufferedReader recReader = new BufferedReader(new FileReader(ctl.getFullResourcePath(recordsFile)));
            String line = null;
            while ((line = recReader.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(line, ",");
                tempResults.add(new RecordEntry(st.nextToken(), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())));
            }
            records = new RecordEntry[tempResults.size()+1];
            tempResults.toArray(records);
            Arrays.sort(records);
            recReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void saveRecords(int currentScore, int currentLevel) {
        if (currentScore == 0) return;
        RecordEntry currentResult = new RecordEntry(currentScore, currentLevel);
        if (records.length < RECORDS_TO_DISPLAY || currentResult.compareTo(records[records.length-2]) == -1) {
            String playerName = JOptionPane.showInputDialog(ctl.getMainWindow(), "Enter player name: ", "Good game! You've set a new high score!", JOptionPane.INFORMATION_MESSAGE);
            if (playerName == null) {
                playerName = "The shy guy";
            }
            currentResult.setPlayer(playerName);
            records[records.length-1] = currentResult;
            Arrays.sort(records);
        }
        
        try {
            PrintWriter recWriter = new PrintWriter(ctl.getFullResourcePath(recordsFile));
            int recSize = (records.length > RECORDS_TO_DISPLAY)? RECORDS_TO_DISPLAY : records.length;
            for (int i = 0; i < recSize; i++) {
                recWriter.println(records[i].getPlayer() + "," + records[i].getScore() + "," + records[i].getLevel());
            }
            recWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private JTextField getDisabledTextField(String text) {
        JTextField result = new JTextField(text);
        result.setEnabled(false);
        return result;
    }
    
    public JPanel initHallOfFame() {
        JPanel result = new JPanel(new BorderLayout());
        result.add(new JLabel("Hall of Fame", SwingConstants.CENTER), BorderLayout.NORTH);
        
        JPanel scoreBoard = new JPanel(new GridLayout(0, 3));
        scoreBoard.add(new JLabel("Player", SwingConstants.CENTER));
        scoreBoard.add(new JLabel("Score", SwingConstants.CENTER));
        scoreBoard.add(new JLabel("Level", SwingConstants.CENTER));
        int recSize = (records.length > RECORDS_TO_DISPLAY)? RECORDS_TO_DISPLAY : records.length - 1;
        for (int i = 0; i < recSize; i++) {
            scoreBoard.add(getDisabledTextField(records[i].getPlayer()));
            scoreBoard.add(getDisabledTextField(new Integer(records[i].getScore()).toString()));
            scoreBoard.add(getDisabledTextField(new Integer(records[i].getLevel()).toString()));
        }
        for (int i = recSize; i < RECORDS_TO_DISPLAY; i++) {
            scoreBoard.add(getDisabledTextField("Your name"));
            scoreBoard.add(getDisabledTextField("Beat 0"));
            scoreBoard.add(getDisabledTextField("1"));
        }
        result.add(scoreBoard, BorderLayout.SOUTH);
        
        return result;
    }
}
